﻿#!/usr/bin/env python
#coding:utf-8
from probe.core.level import *
import commands
def get_flow_info(args):
    '''针对网卡监控网络流量'''
    #如果网卡不存在，获取的数据为n/a，则返回结果将用-1表示
    network = args.get('network')
    result = commands.getoutput('ifstat -i %s 1 1|grep -Ev "%s|KB/s"'%(network,network))
    if 'n/a' in result:
        flow_in = flow_out = -1
        return WarningData({network+' is not exist': [float(flow_in), 'KB/s']})
    flow_in, flow_out = result.split()
    if float(flow_in) >= args.get('WarningData') or float(flow_out) >= args.get('WarningData'):
        return WarningData({'%s流入流量'%network: [float(flow_in), 'KB/s'], '%s流出流量'%network: [float(flow_out), 'KB/s']})
    return InfoData({'%s流入流量'%network: [float(flow_in), 'KB/s'], '%s流出流量'%network: [float(flow_out), 'KB/s']})
if __name__ == '__main__':
    print get_flow_info({"WarningData": 10,"network": "eth0"})
